var doc;

var awplus_prefs = Components.classes["@mozilla.org/preferences-service;1"].
                getService(Components.interfaces.nsIPrefBranch);

function do_awplus(e) {
  if (awplus_prefs.getBoolPref('awplus.disabled')) return;

  var location = e.originalTarget.location.href;
  if (/^http:\/\/www1\.astrowars\.com\/0\/.*$/.test(location)) {
    doc = e.originalTarget;

    ap_add_script("libs/jquery-1.2.6.pack.js");
    ap_add_script("libs/jquery-ui-personalized-1.5.2.packed.js");
    ap_add_script("libs/awplus.js");
    ap_add_script("plugins/systemplayer_dialog.js");
    
    if (/News\/$/.test(location)) {
      ap_add_script("plugins/news.js");
    }
    else if (/Map/.test(location)) {
      ap_add_script("plugins/map.js");
    }
    else if (/Planets\/$/.test(location)) {
      ap_add_script("plugins/planet.js");
    }
    else if (/Planets\/Detail.php\/\?i=[0-9]+$/.test(location)) {
      ap_add_script("plugins/planetdet_levelup.js");
      ap_add_script("plugins/planetdet_sethome.js");
    }
    else if (/Planets\/Buildings.php$/.test(location)) {
      ap_add_script("plugins/planetbuild_possible.js");
    }
    else if (/\/Science\/$/.test(location)) {
      ap_add_script("plugins/science.js");
    }
    else if (/\/Trade\/$/.test(location)) {
      if (awplus_prefs.getBoolPref('awplus.trade_quick_buy')) {
        ap_add_script("plugins/trade.js");
      }
    }
    else if (/Alliance\/$/.test(location)) {
      ap_add_script('plugins/alliance.js');
    }
    else if (/Alliance\/List.php$/.test(location)) {
      ap_add_script('plugins/alliance_list.js');
    }
    else if (/Alliance\/Detail.php\/\?id=[0-9]+$/.test(location)) {
      ap_add_script('plugins/alliance_memberlist.js');
      ap_add_script('plugins/alliance_fleetscv.js');
    }
    else if (/Fleet\/$/.test(location)) {
      ap_add_script('plugins/fleets.js');
    }
    else if (/Launch\.php/.test(location)) {
      ap_add_script("plugins/launch_systemonselect.js");
      ap_add_script("plugins/launch_traveltime.js");
      if (awplus_prefs.getBoolPref('awplus.launch_check')) {
        ap_add_script("plugins/launch_check.js");
      }
      ap_add_script("plugins/launch_quickdestinations.js");
      ap_add_script("plugins/launch_fleetparcels.js");
    }

    ap_add_script("libs/awplus_finalizer.js");
  }
}

function ap_add_script(aURL) {
  aURL = "chrome://awplus/content/" + aURL;
  var scriptElm = doc.createElement("script");
  var text = ap_getContents(aURL) + "\n\n";
  scriptElm.appendChild(doc.createTextNode(text));
  doc.body.appendChild(scriptElm);
  doc.body.removeChild(scriptElm);
}

function ap_getContents(aURL){
  var ioService=Components.classes["@mozilla.org/network/io-service;1"]
    .getService(Components.interfaces.nsIIOService);
  var scriptableStream=Components
    .classes["@mozilla.org/scriptableinputstream;1"]
    .getService(Components.interfaces.nsIScriptableInputStream);
  var channel=ioService.newChannel(aURL,null,null);
  var input=channel.open();
  scriptableStream.init(input);
  var str=scriptableStream.read(input.available());
  scriptableStream.close();
  input.close();
  return str;
}

window.addEventListener("load", function() {
  initAwPlus();
  var appcontent = window.document.getElementById("appcontent");
  if (appcontent) {
    if (!appcontent.greased_awplus) {
      appcontent.greased_awplus = true;
      appcontent.addEventListener("DOMContentLoaded", do_awplus, false);
    }
  }
}, false);

function toggleAwPlus(o) {
  var option = o.getAttribute("checked") ? true : false;
  awplus_prefs.setBoolPref('awplus.disabled', option);
}

function toggleAwPlusItens (o, p) {
  var option = o.getAttribute("checked") ? true : false;
  awplus_prefs.setBoolPref(p, option);
}

function ap_openAwUrl(url) {
  var u = 'http://www1.astrowars.com'+url;
  ap_openUrl(u);
}

function ap_openUrl(url) {
  gBrowser.loadURI(url);
}

function initAwPlus() {
  document.getElementById('apDisabled').setAttribute("checked", awplus_prefs.getBoolPref('awplus.disabled'));
  document.getElementById('apLaunchCheck').setAttribute("checked", awplus_prefs.getBoolPref('awplus.launch_check'));
  document.getElementById('apTradeQuickBuy').setAttribute("checked", awplus_prefs.getBoolPref('awplus.trade_quick_buy'));
}

